package com.it.dao.impl;

import com.it.dao.AccountDao;
import com.it.pojo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public Account findById(Integer id) {//单行多列
        Account account = jdbcTemplate.queryForObject("select * from account where id=?", new BeanPropertyRowMapper<Account>(Account.class), id);
        return account;
    }

    public void transferOut(Integer id, Float money) {
        String sql="update account set money=money-? where id=?";
        int update = jdbcTemplate.update(sql,money,id);
        System.out.println("update = " + update);
    }

    public void transferIn(Integer id, Float money) {
        String sql="update account set money=money+? where id=?";
        int update = jdbcTemplate.update(sql, money,id);
//        int i=10/0;
        System.out.println("update = " + update);

    }
}
